// 获取图片像素
export function getImgRatio(file) {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function() {
      if (reader.readyState == 2) {
        const img = new Image();
        img.src = reader.result;
        img.onload = function() {
          resolve({
            width: this.width,
            height: this.height,
          });
        };
      }
    };
  });
}

/**
 * base64转Blob
 * @param {*} data 
 */
 export const base64ByBlob = (base64, callback) => {
  let arr = base64.split(','), mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  callback(new Blob([u8arr], { type: mime }))
}

export function fileToBase64(file) {
  return new Promise(resolve => {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e) {
      resolve(e.target.result);
    };
  })
};

export function saveShareContent (blobUrl, fileName) {
  let downLink = document.createElement('a')
  downLink.download = fileName
  downLink.href = blobUrl
  // 链接插入到页面
  document.body.appendChild(downLink)
  downLink.click()
  // 移除下载链接
  document.body.removeChild(downLink)
}
